package cn.codesea.housework_user.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnOperationClickListener;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickOrderListener;
import cn.codesea.housework_user.entity.Order;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.entity.Worker;

public class OrderRcvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    LayoutInflater mInflater;
    List<Order> orders;


    //声明自定义的监听接口
    private OnRecyclerItemClickOrderListener monItemClickListener;

    private OnOperationClickListener listener;

    //提供set方法供Activity或Fragment调用
    public void setRecyclerItemClickListener(OnRecyclerItemClickOrderListener listener){
        monItemClickListener=listener;
    }

    public void setOperationClickListener(OnOperationClickListener listener){
        this.listener = listener;
    }


    public OrderRcvAdapter(Context context, List<Order> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        orders = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = mInflater.inflate(R.layout.item_recycler_order, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ItemViewHolder){

            Order order = orders.get(position);
            Service service = order.getService();
            Worker worker = order.getWorker();
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            String ser_time = order.getService_starttime() + " 至 " + order.getService_endtime();
            itemViewHolder.serTime_tv.setText(ser_time);
            itemViewHolder.serviceName_tv.setText(service.getName());

            if (worker != null){
                itemViewHolder.workerName_tv.setText(worker.getName());
                itemViewHolder.workerPhone_tv.setText(worker.getPhone());
            }

            if (orders.size()>0){
                int status = orders.get(0).getStatus();
                switch (status){
                    case 0:
                        itemViewHolder.operation_tv.setText("取消");
                        itemViewHolder.operation_tv.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        itemViewHolder.operation_tv.setVisibility(View.GONE);
                        break;
                    case 2:
                        itemViewHolder.operation_tv.setText("结束");
                        itemViewHolder.operation_tv.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        itemViewHolder.operation_tv.setText("评价");
                        itemViewHolder.operation_tv.setVisibility(View.VISIBLE);
                        break;
                    case 4:
                        itemViewHolder.operation_tv.setVisibility(View.GONE);
                        break;
                }
            }

        }

    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cardView)
        CardView cardView;
        @BindView(R.id.operation_tv)
        TextView operation_tv;// 根据订单状态（未接单、已接单、进行中、已结束）来显示
        @BindView(R.id.serviceName_tv)
        TextView serviceName_tv;
        @BindView(R.id.serTime_tv)
        TextView serTime_tv;
        @BindView(R.id.workerName_tv)
        TextView workerName_tv;
        @BindView(R.id.workerPhone_tv)
        TextView workerPhone_tv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (monItemClickListener!=null){
                        monItemClickListener.onItemClick(getAdapterPosition(),orders);
                    }
                }
            });

            operation_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onOperationClick(getAdapterPosition(),orders);
                    }
                }
            });

        }

    }

}
